package com.oktest.measure.ui.adapter

import com.chad.library.adapter.base.BaseNodeAdapter
import com.chad.library.adapter.base.entity.node.BaseNode
import com.oktest.measure.R
import com.oktest.measure.entity.Device
import com.oktest.measure.entity.Measure
import com.oktest.measure.entity.Type
import com.oktest.measure.item.work.provider.WorkDeviceProvider
import com.oktest.measure.item.work.provider.WorkSub1Provider
import com.oktest.measure.item.work.provider.WorkSub2Provider
import com.oktest.measure.item.work.provider.WorkTypeProvider

/**
 * @Author hhh
 * @Date 2020/9/22
 */
class WorkTreeAdapter:BaseNodeAdapter() {

    init {
        addNodeProvider(WorkDeviceProvider())
        addNodeProvider(WorkTypeProvider())
        addNodeProvider(WorkSub1Provider())
        addNodeProvider(WorkSub2Provider())
        addChildClickViewIds(R.id.sub1BoxCheck)
        addChildClickViewIds(R.id.sub2BoxCheck)
        addChildClickViewIds(R.id.tvFeedback)
        addChildClickViewIds(R.id.etValue)
    }

    override fun getItemType(data: List<BaseNode>, position: Int): Int {
        return when (val node = data[position]) {
            is Device -> 1
            is Type -> 2
            is Measure -> if (node.isTelemetry()) 3 else 4
            else -> 0
        }
    }

    fun close(node: BaseNode?) {
        collapse(getItemPosition(node), parentPayload = EXPAND_COLLAPSE_PAYLOAD)
    }

    fun open(node: BaseNode?) {
        expand(getItemPosition(node), parentPayload = EXPAND_COLLAPSE_PAYLOAD)
    }

    companion object{
        const val EXPAND_COLLAPSE_PAYLOAD = 110
    }
}